#include "../ocl/chunk.h"


ocl_chunk * ocl_chunk_create( size_t size )
{
	const size_t total = sizeof(ocl_chunk) + size;
	char        *array = MDS_CALLOC_AS(char,total);
	ocl_chunk   *chunk = (ocl_chunk *)array;
	
	ocl_errno = CL_SUCCESS;
	
	if( !chunk)
		return NULL;
	
	if( size>0 )
	{
		chunk->data = &array[sizeof(ocl_chunk)];
		chunk->size = size;
		if( ocl_logfile ) fprintf( stderr, "[ocl_chunk_create] @%p+<%u>\n", chunk->data, (unsigned)(chunk->size) );

	}
	
	return chunk;
}

void ocl_chunk_delete( ocl_chunk *chunk )
{
	assert( chunk != NULL );
	assert( !(chunk->data == NULL && chunk->size>0) );
	if( ocl_logfile ) fprintf( stderr, "[ocl_chunk_delete] @%p+<%u>\n", chunk->data, (unsigned) (chunk->size) );
	MDS_FREE_AS(char,chunk,sizeof(ocl_chunk)+chunk->size);
}


int ocl_chunk_write( const ocl_chunk *chunk, FILE *fp )
{
	assert( fp );
	assert( chunk != NULL );
	assert( !(chunk->data == NULL && chunk->size>0) );
	
	ocl_errno = CL_SUCCESS;
	if( fwrite(chunk->data, 1, chunk->size, fp) != chunk->size )
		return -1;
	
	return 0;
}


